package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.userService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(("/user/"))
public class UserController {
    @Autowired
    private userService userSerive;

    @GetMapping("{id}")
    public SysResult findByUSerId (@PathVariable Integer id){
        User msg = userSerive.fandByUserId(id);
        return SysResult.success(msg);
    }

    @PutMapping("updateUser")
    public SysResult updateUser(@RequestBody User user){
        userSerive.updateUser(user);
        return SysResult.success();
    }

    @PostMapping("addUser")
    public SysResult addUser(@RequestBody User user){
        System.out.println(user);
        userSerive.addUser(user);
        return SysResult.success();
    }

    @DeleteMapping("deleteUser/{id}")
    public SysResult deleteUser(@PathVariable Integer id) {
        userSerive.deleteUser(id);
        return SysResult.success();
    }

    @PutMapping("status/{id}/{status}")
    public SysResult updateState(User user) {
        System.out.println(user.getId() + "," + user.getStatus());
        userSerive.updateState(user);
        return SysResult.success();
    }

    @GetMapping("list")
    public SysResult getUserList(PageResult pageResult) {
        pageResult = userSerive.getUserList(pageResult);
        return SysResult.success(pageResult);
    }

    @GetMapping("findAll")
    public List<User> findAll() {
        List<User> list = userSerive.findAll();
        return list;
    }

    @PostMapping("login")
    public SysResult login(@RequestBody User user) {
        //执行后端操作返回秘钥
        String token = userSerive.login(user);
        //判断返回值是否有效
        if (StringUtils.hasLength(token)) {
            return SysResult.success(token);
        } else {
            return SysResult.fail();
        }
    }


}
